#!/bin/bash

# __/\\\\\\\\\\\\\__________/\\\\\\___________/\\\\\\\\\\\\\__________/\\\\\\__________        
#  _\/\\\/////////\\\_______\////\\\__________\/\\\/////////\\\_______\////\\\__________       
#   _\/\\\_______\/\\\__/\\\____\/\\\_____/\\\_\/\\\_______\/\\\__/\\\____\/\\\_____/\\\_      
#    _\/\\\\\\\\\\\\\\__\///_____\/\\\____\///__\/\\\\\\\\\\\\\\__\///_____\/\\\____\///__     
#     _\/\\\/////////\\\__/\\\____\/\\\_____/\\\_\/\\\/////////\\\__/\\\____\/\\\_____/\\\_    
#      _\/\\\_______\/\\\_\/\\\____\/\\\____\/\\\_\/\\\_______\/\\\_\/\\\____\/\\\____\/\\\_   
#       _\/\\\_______\/\\\_\/\\\____\/\\\____\/\\\_\/\\\_______\/\\\_\/\\\____\/\\\____\/\\\_  
#        _\/\\\\\\\\\\\\\/__\/\\\__/\\\\\\\\\_\/\\\_\/\\\\\\\\\\\\\/__\/\\\__/\\\\\\\\\_\/\\\_ 
#         _\/////////////____\///__\/////////__\///__\/////////////____\///__\/////////__\///__

if [[ ! $0 == /bin/bash ]];then

# ===================================================== #
url="https://wiki.biligame.com/ys"			# 哔哩哔哩游戏百科
home="$(dirname ${BASH_SOURCE[0]})" #主目录
path="$home/genshin_impact"	# 目标下载目录
source $home/config/genshin_impact_role_list_array.sh		# 读取角色列表
# ===================================================== #



f=0
clear

[ ! -d $path/text ] && mkdir -pv $path/text
echo -e "\a **开始从 \e[1;36m$url\e[0m 获取文本元素(不包括旅行者)**"
for i in ${role_list[@]}; do
	b=0
	if [ ! -f $path/text/$i.txt ];then
	echo -en "\r - 正在获取网页源码 \e[1;36m[$i]\e[0m"
	html=`curl -sL $url/${i}语音`
	echo -en "\r - 正在获取文本内容 \e[1;36m[$i]\e[0m"
	video_text=(`cat <<< $html | grep '<td colspan="4"><div class="voice_text_chs_m vt_active_m">' | sed 's/<\/div><div class="voice_text_jp_m" lang="jp">/\n/g;s/<\/div><div class="voice_text_en_m" lang="en">/\n/g;s/<\/div><div class="voice_text_kr_m" lang="kr">/\n/g;s/<\/div>//g;s/<td colspan="4"><div class="voice_text_chs_m vt_active_m">//g;s/<div class="voice_text_jp_m">/\n/g;s/<div class="voice_text_en_m">/\n/g;s/<div class="voice_text_kr_m">/\n/g' | sed 's/ /_/g'`)
	video_text_length=${#video_text[@]}
 	echo -en "\r - 正在获取文本标题 \e[1;36m[$i]\e[0m"
	video_title=(`cat <<< $html | grep '<th colspan="4">' | sed 's/<th colspan="4"><div class="voice_lang_m">文本语言：<div class="voice_lang_button_m vb_active_m" id="CHS_m">中<\/div><div class="voice_lang_button_m" id="JP_m">日<\/div><div class="voice_lang_button_m" id="EN_m">英<\/div><div class="voice_lang_button_m" id="KR_m">韩<\/div><\/div>//g' | sed 's/<th colspan="4">//g'`)
	video_title_length=${#video_title[@]}
		for ((v=0; v<$video_text_length; v+=4)); do
			echo "${video_title[b]}" >> $path/text/$i.txt
			let b++
    			for ((j=$v; j<v+4 && j<$video_text_length; j++)); do  
        			echo "${video_text[j]}" | sed 's/_/ /g' >> $path/text/$i.txt 
    			done  
   			echo "-------------------" >> $path/text/$i.txt
   			echo -ne "\r - \e[1;32m[$b]\e[0m 正在写入语音文本内容 \e[1;36m[$i]\e[0m: "
		done
		echo  -ne "\e[1;32m完成。\e[0m"
	else
		echo -ne " - 写入语音文本内容 \e[1;36m[$i]\e[0m: \e[1;33m跳过。\e[0m"
	fi
	let f++
	echo -e "第 \e[1;32m$f\e[0m 个角色，共 \e[1;32m${#role_list[@]}\e[0m 个角色"
	sleep 0.01
done

[ ! -d $path/video ] && mkdir -pv $path/video
echo -e "\a **开始从 \e[1;36m$url\e[0m 获取下载链接**"
for i in ${role_list[@]}; do
	content=0
	echo -en "\r - 正在获取下载地址 \e[1;36m[$i]\e[0m                                                         "
	video_all=(`curl -sL $url/${i}语音 | grep '^<td style="width:25%"><div class="bikit-audio" data-src=' | grep -o -E "\"https?&#58;//patchwiki\.biligame\.com/images/ys/[0-9a-z]/[0-9a-z]+/[0-9a-z]+\.(mp3|ogg)\"" | sed 's/&#58;/:/g;s/"//g'`)
	for v in ${video_all[@]}; do
		if [ ! -f $path/video/$i/$(echo $v | awk -F/ '{print $NF}') ];then
			[ ! -d $path/video/$i ] && mkdir $path/video/$i
			wget -P $path/video/$i/ $v 2>/dev/null &&
			let content++ &&
			echo -ne "\r - 已下载 \e[1;36m[$i]\e[0m 语音 \e[1;32m$(echo $v | awk -F/ '{print $NF}')\e[0m 第 \e[1;32m$content\e[0m 个文件，共 \e[1;32m${#video_all[@]}\e[0m 个文件   "
		else
			echo -ne "\r - 跳过下载 \e[1;36m[$i]\e[0m 语音 \e[1;32m$(echo $v | awk -F/ '{print $NF}')\e[0m                               "
		fi
	done
	content=0
done

fi
